package cn.luoshaocheng.org.service.impl;

import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.service.impl.BaseServiceImpl;
import cn.luoshaocheng.basic.util.BaiduAuditUtils;
import cn.luoshaocheng.org.domain.Employee;
import cn.luoshaocheng.org.domain.Shop;
import cn.luoshaocheng.org.domain.ShopLog;
import cn.luoshaocheng.org.dto.ShopDto;
import cn.luoshaocheng.org.mapper.EmployeeMapper;
import cn.luoshaocheng.org.mapper.ShopMapper;
import cn.luoshaocheng.org.mapper.ShoplogMapper;
import cn.luoshaocheng.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    /* *商家入驻逻辑步骤：
     * 一：检验
     * 1.1.每个参数的空值判断
     * 1.2.两次密码不一致校验
     * 1.3.该店铺是否已经被入驻过
     * 二：入驻业务
     * 2.1.保存店铺管理员信息t_employee
     * 2.2.保存店铺信息t_shop
     * 2.2.将店铺的id更新到t_employee中*/

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShoplogMapper  shoplogMapper;
    @Autowired
    private JavaMailSender javaMailSender;


    @Override
    public void settlement(Shop shop) {
        //每个参数的空值判断
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getLogo())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ) {
            throw new BusinessException("店铺名称不能为空!");
        }

        //通过百度ai审核店铺的名字是否违规
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){
                throw  new BusinessException("店铺名称违规");
        }

        //两次密码不一致校验
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("密码错误，请重新输入");
        }
        //该店铺是否已经被入驻过
        Shop dbshop = shopMapper.getShopByMessage(shop);
        if (dbshop != null) {
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }

        //保存店铺管理员信息t_employee
        employeeMapper.save(admin);


        //保存店铺信息t_shop
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);

        //将店铺的id更新到t_employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void pass(ShopLog log) throws MessagingException {
        //1修改状态  通过shop id查询 shop数据
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核通过
        shop.setState(2);
        shopMapper.update(shop);
        //2录入日志
        log.setState(2);
        log.setAudit_id(1L);
        shoplogMapper.save(log);

        // 通知商家
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("354374118@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("aa.jpg",new File("C:\\Users\\kevin\\Pictures\\Saved Pictures\\aa.jpg"));
        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("1758619926@qq.com");
        javaMailSender.send(mimeMessage);

    }

    @Override
    public void reject(ShopLog log) throws MessagingException {
        //1修改状态  通过shop id查询 shop数据
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核通过
        shop.setState(4);
        shopMapper.update(shop);
        //2录入日志
        log.setState(4);
        log.setAudit_id(1L);
        shoplogMapper.save(log);

        // 通知商家
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("354374118@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("aa.jpg",new File("C:\\Users\\kevin\\Pictures\\Saved Pictures\\aa.jpg"));
        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("1758619926@qq.com");
        javaMailSender.send(mimeMessage);

    }

    @Override
    public List<ShopDto> getCountByState() {
        return  shopMapper.getEcharts();
    }
}

